//
//  ZWNewFeatureViewControlle.swift
//  张伟的微博
//
//  Created by 伟 on 16/3/2.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
let itemCount = 4

class ZWNewFeatureViewControlle: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(ZWNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true // 分页滚动


    }
    private var layout :UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout:layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

  
   

}
extension ZWNewFeatureViewControlle {

//  返回cell的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    //返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ZWNewFeatureCell
        
        cell.itemIdex = indexPath.item
        return cell
    }
//当页面滚动到最后一张时
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        if index == itemCount - 1 {
            
            let visibleIndexPath = NSIndexPath(forItem: index, inSection: 0)
            if let cell = collectionView?.cellForItemAtIndexPath(visibleIndexPath) as? ZWNewFeatureCell {
                cell.startButtonAnm()
            }
        }
        
    }

}



class ZWNewFeatureCell :UICollectionViewCell {
    
    
    //计算型属性
    var itemIdex : Int = 0 {
        didSet {
            bkgView.image = UIImage(named: "new_feature_\(itemIdex + 1)")
            startButton.hidden = true
        }
    }
    
    func startButtonAnm() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1,1)
            
            }) { (_) -> Void in
                
        }
    }
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置UI
    func prepareUI() {
        contentView.addSubview(bkgView)
        contentView.addSubview(startButton)
        //设置bkgview的约束
        //背景约束
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(EdgeInsetsZero)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(bkgView).offset(-160)
        }
        
//        bkgView.translatesAutoresizingMaskIntoConstraints = false
//        startButton.translatesAutoresizingMaskIntoConstraints = false
//        contentView.addConstraints(
//            NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgView]))
//        contentView.addConstraints(
//            NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgView]))
//        
        
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bkgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
         contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bkgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
        
        
    }
    
    //MARK: -懒加载
    lazy var bkgView :UIImageView  = UIImageView(image: UIImage(named: "new_feature_1"))
    lazy var startButton :UIButton = {
    
        let startButton = UIButton()
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        startButton.setTitle("开始微博", forState: UIControlState.Normal)
        startButton.sizeToFit()
        
        startButton.addTarget(self, action: "goMainController", forControlEvents: UIControlEvents.TouchUpInside)
        return startButton
    }()
    func goMainController (){
        AppDelegate.outSwitchViewController(false)
    
    }
    
}
